/**create by framework at 2019年05月30日 15:47:03**/
package com.star.truffle.module.fzjz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.star.truffle.common.constants.EnabledEnum;
import com.star.truffle.core.StarServiceException;
import com.star.truffle.core.jdbc.Page;
import com.star.truffle.core.jdbc.Page.OrderType;
import com.star.truffle.core.web.ApiCode;
import com.star.truffle.core.web.ApiResult;
import com.star.truffle.module.fzjz.dto.req.WarInfoRequestDto;
import com.star.truffle.module.fzjz.dto.res.WarCategoryResponseDto;
import com.star.truffle.module.fzjz.dto.res.WarInfoResponseDto;
import com.star.truffle.module.fzjz.service.WarCategoryService;
import com.star.truffle.module.fzjz.service.WarInfoService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/warInfo")
public class WarInfoController {

  @Autowired
  private WarInfoService warInfoService;
  @Autowired
  private WarCategoryService warCategoryService;

  @ResponseBody
  @RequestMapping(value = "/map", method = {RequestMethod.POST, RequestMethod.GET})
  public ApiResult<Map<Long, List<WarInfoResponseDto>>> map(@RequestBody WarInfoRequestDto warInfoRequestDto) {
    try {
//      URI uri = new URI("https://image.cnhnb.com/group1/M00/66/6E/F53DA8O75EChRkRljOUGKAWBwUAAEt-9JB-QA57.JPEG");
//      InputStream is = uri.toURL().openStream();
      warInfoRequestDto.setEnabled(EnabledEnum.enabled.val());
      List<WarInfoResponseDto> list = warInfoService.queryWarInfo(warInfoRequestDto);
      Map<Long, List<WarInfoResponseDto>> map = list.stream().collect(Collectors.groupingBy(WarInfoResponseDto::getCateId, Collectors.mapping(dto -> dto, Collectors.toList())));
      return ApiResult.success(map);
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
  
  @ResponseBody
  @RequestMapping(value = "/add", method = RequestMethod.POST)
  public ApiResult<Long> add(@RequestBody WarInfoRequestDto warInfo) {
    try {
      Long id = warInfoService.saveWarInfo(warInfo);
      return ApiResult.success(id);
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
  
  @ResponseBody
  @RequestMapping(value = "/adds/{cateId}", method = RequestMethod.POST)
  public ApiResult<Void> add(@PathVariable Long cateId, @RequestBody List<WarInfoRequestDto> warInfos) {
    try {
      warInfoService.saveWarInfos(cateId, warInfos);
      return ApiResult.success();
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
  
  @RequestMapping(value = "/lists/{cateId}", method = RequestMethod.GET)
  public String list(@PathVariable Long cateId, Model model) {
    WarCategoryResponseDto warCategoryResponseDto = this.warCategoryService.getWarCategory(cateId);
    model.addAttribute("cateDto", warCategoryResponseDto);
    return "mgr/warInfo/list";
  }
  
  @RequestMapping(value = "/addBefore/{cateId}", method = RequestMethod.GET)
  public String addBefore(@PathVariable Long cateId, Model model) {
    WarCategoryResponseDto warCategoryResponseDto = this.warCategoryService.getWarCategory(cateId);
    model.addAttribute("cateDto", warCategoryResponseDto);
    return "mgr/warInfo/addWarInfo";
  }

  @RequestMapping(value = "/editBefore/{id}", method = RequestMethod.GET)
  public String editBefore(@PathVariable Long id, Model model) {
    WarInfoResponseDto warInfoResponseDto = this.warInfoService.getWarInfo(id);
    model.addAttribute("warInfoResponseDto", warInfoResponseDto);
    return "mgr/warInfo/editWarInfo";
  }

  @ResponseBody
  @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET})
  public Map<String, Object> list(WarInfoRequestDto warInfoRequestDto, Integer page, Integer rows, String sord, String sidx) {
    Page pager = new Page(page, rows, sidx, OrderType.desc.name().equals(sord) ? OrderType.desc : OrderType.asc);
    warInfoRequestDto.setPager(pager);
    Map<String, Object> map = new HashMap<>();
    Long count = warInfoService.queryWarInfoCount(warInfoRequestDto);
    map.put("page", pager.getPageNum());
    map.put("total", count % pager.getPageSize() == 0 ? count / pager.getPageSize() : count / pager.getPageSize() + 1);
    map.put("records", count);
    if(count > 0) {
      List<Map<String, Object>> list = warInfoService.queryWarInfoForMap(warInfoRequestDto);
      map.put("rows", list);
    }
    return map;
  }

  @ResponseBody
  @RequestMapping(value = "/edit", method = RequestMethod.POST)
  public ApiResult<Void> edit(@RequestBody WarInfoRequestDto warInfoRequestDto) {
    try {
      warInfoService.updateWarInfo(warInfoRequestDto);
      return ApiResult.success();
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
  
  @ResponseBody
  @RequestMapping(value = "/deleted", method = RequestMethod.POST)
  public ApiResult<Void> delete(String ids) {
    try {
      warInfoService.deleteWarInfo(ids);
      return ApiResult.success();
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }

  @ResponseBody
  @RequestMapping(value = "/delDatas/{cateId}", method = RequestMethod.POST)
  public ApiResult<Void> delDatas(@PathVariable Long cateId) {
    try {
      warInfoService.delDatas(cateId);
      return ApiResult.success();
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }

}